﻿@model IEnumerable<Common.Views.OrdersView>

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Index</h2>
<script type="text/javascript">
    function addComment(inc, id) {
        if ($("#comment" + inc + "").val() == "") {
            alert("Comment cannot be empty. Please input a valid comment.");
        }
        else {
            var param = {
                productID: id,
                comment: $('#comment' + inc + '').val()
            };
            $.ajax({
                url: '@Url.Action("AddComment")',
                type: "POST",
                datatype: "json",
                data: param,
                error: function (xmlHttpRequest, errorText, thrownError) {
                    alert(xmlHttpRequest + "|" + "This comment could not be added." + "|" + thrownError);
                },
                success: function (data) {
                    if (data != null) {
                        alert("Comment has been added!");
                    }
                }
            });
        }
    }

    function addrating(inc, id) {
        var param = {
            productID: id,
            rating: $('#rate' + inc + '').val()
        };
        $.ajax({
            url: '@Url.Action("AddRating")',
            type: "POST",
            datatype: "json",
            data: param,
            error: function (xmlHttpRequest, errorText, thrownError) {
                alert(xmlHttpRequest + "|" + "This product could not be added." + "|" + thrownError);
            },
            success: function (data) {
                var msg = data.hasOwnProperty("rated") ? data.d : data;

                if (msg == "rated") {
                    alert("You have already rated this product!");
                }
                else {
                    alert("Your rating has been successfully recorded!");
                }
            }
        });
    }

    function savedetails() {
        $.ajax({
            url: '@Url.Action("SaveDetails")',
            type: "POST",
            datatype: "json",
            data: null,
            error: function (xmlHttpRequest, errorText, thrownError) {
                alert(xmlHttpRequest + "|" + "The details could not be saved" + "|" + thrownError);
            },
            success: function (data) {
                if (data != null) {
                    alert("The details have been sent to you over e-mail!");
                }
            }
        });
    }

    function printdetails() {
        $.ajax({
            url: '@Url.Action("PrintDetails")',
            type: "GET",
            datatype: "json",
            data: null,
            error: function (xmlHttpRequest, errorText, thrownError) {
                alert(xmlHttpRequest + "|" + errorText + "|" + thrownError);
            },
            success: function (data) {
                if (data != null) {
                    window.location = "Orders/PrintDetails";
                }
            }
        });
    }
</script>

<table>
    <tr>
        <th>Product Name</th>
        <th>Product Image</th>
        <th>Price</th>
        <th>Add Rating</th>
        <th>Add Comment</th>
        <th>More Details</th>
    </tr>

    @{
        int inc = 1;
     }

    @foreach (var item in Model)
    {
        <tr>
        <td>
            @Html.DisplayFor(modelItem => item.ProductName)
            @Html.HiddenFor(modelItem => item.ProductID, new { id = "prodid" })
        </td>
        <td>
            <img height="150px" width="150px" src= "@Url.Content(item.ImageLink)" alt="@Html.DisplayFor(modelItem => item.ImageLink)" />
        </td>
        <td>
            €@Html.DisplayFor(modelItem => item.UnitPrice)
        </td>
        <td>
            @{
                    List<SelectListItem> ratings = new List<SelectListItem>();
                    for(int i = 1; i <= 5; i++)
                    {
                        ratings.Add(new SelectListItem { Text = i.ToString(), Value = i.ToString()});
                    }
         
                    @Html.Label("Rating:")
                    @Html.DropDownList("rate" + inc + "", ratings)
            }
            <button type="button" onclick="addrating(@inc, @item.ProductID)">Add Rating</button>
        </td>
        <td> 
             @Html.TextBox("comment" +inc + "")
             <button type="button" onclick="addComment(@inc, @item.ProductID)">Add Comment</button>
        </td>
        <td>
            @Ajax.ActionLink("View Product Details", "GetProductDetails", new { id = item.ProductID, oID = item.OrderID },
                new AjaxOptions
                {
                    HttpMethod = "GET",
                    UpdateTargetId = inc.ToString(),
                    InsertionMode = InsertionMode.Replace
                })
                <br />
                <div id='@inc'></div>
        </td>
        </tr>
        {
            inc++;
        }
    }
</table>

<button type="button" onclick="printdetails()">Print Details</button>
<button type="button" onclick="savedetails()">Save Details</button>